package com.syj.web.controller;

import com.syj.core.result.ReturnData;
import com.syj.core.variable.SessionUtil;
import com.syj.web.model.ProductModel;
import com.syj.web.model.WorkData;
import com.syj.web.model.Works;
import com.syj.web.service.ProductModelService;
import com.syj.web.service.WorkDataService;
import com.syj.web.service.WorksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by rankawin on 2017/2/12.
 */
@Controller
@RequestMapping("/works")
public class WorksController {

    @Autowired
    private ProductModelService productModelService;

    @Autowired
    private WorksService worksService;

    @RequestMapping(value = "/{productId}", method = RequestMethod.GET)
    public String worksPage(@PathVariable Integer productId, ModelMap map) {
        map.put("productModel", productModelService.selectModel(productId));
        ProductModel productModel = new ProductModel();
        return "user_herstellung";
    }

    @RequestMapping(value = "/{worksId}", method = RequestMethod.POST)
    @ResponseBody
    public ReturnData addWorks(@RequestParam(value = "file", required = false) MultipartFile[] file,
                               HttpServletRequest request, @PathVariable("worksId") Integer worksId) {
        worksId = worksId == null ? 0 : worksId;
        return new ReturnData().setData(productModelService.addWorks(file, request, worksId));
    }

    @RequestMapping(value = "/update", params = "worksId", method = RequestMethod.POST)
    public String addWorksSure(@RequestParam("worksId") Integer worksId,
                               @RequestParam("worksName") String worksName,
                               @RequestParam("worksInfo") String worksInfo) {
        Works works = new Works();
        works.setId(worksId);
        works.setName(worksName);
        works.setInfo(worksInfo);
        worksService.updateSelectiveByPrimaryKey(works);
        return "user_success";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String selectWorks(ModelMap map) {
        Works work = new Works();
        work.setUserId(SessionUtil.getUser().getId());
        List<Works> worksList = worksService.select(work);
        map.put("works", worksList);
        return "user_work";
    }
}
